package com.langsin.sunzhaofa;

import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.sunzhaofa.mapper.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;


public class MyTest {

    SqlSession sqlSession=null;


//map封装
    @Test
    public void testMap() throws Exception{
        HashMap<String,Object> map = new HashMap<>();

        map.put("roleCode", "plain");
        map.put("address", "济南浪曦");

       UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.queryUserByMap(map);

        users.forEach(System.out::println);
    }

    //创建对象封装
    @Test
    public void testVO() throws Exception{

        QueryVo vo = new QueryVo();
        vo.setRoleCode("manager");
        vo.setAddress("济南浪曦");

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryByRoleAndAddress(vo);

        users.forEach(System.out::println);
    }

    //类属性与数据库列名不同时使用
    @Test
    public void testTea() throws Exception{
        Integer tid = 1009;
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.queryByID(tid);
        System.out.println(teacher);

    }

    @Test
    //驼峰测试
    public void textUserById(){
        Integer uid=702;
        UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(uid);
        System.out.println(user);

    }
    @Test
    public void testCounts(){
        //        查询表记录数目
        //         拼接可用
        String tabName="worker";
        GoodsMapper goodsMapper =sqlSession.getMapper(GoodsMapper.class);
        int counts = goodsMapper.counts(tabName);
        System.out.println(tabName + "=" + counts);

    }

    @Test
    public void testqueryByName2(){

//        字符串拼接
        String name="鼠标";
        GoodsMapper goodsMapper =sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName2(name);
        goods.forEach(System.out::println);

    }
    @Test
    public void testqueryByName(){
//        模糊查询
        String name="%鼠标%";
        GoodsMapper goodsMapper =sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName(name);
        goods.forEach(System.out::println);

    }

    @Test
    public void testAddGoods(){
        Goods goods=new Goods();

        goods.setGoodsName("有线鼠标");
        goods.setShopPrice(99.99);

        GoodsMapper goodsMapper =sqlSession.getMapper(GoodsMapper.class);

        int i=goodsMapper.addGoods(goods);
        System.out.print("返回值----------------------------------->");
        System.out.println(i);
        System.out.println(goods.toString());

    }


    @Test
    public void testMapperAdd2() throws Exception {
        Worker worker = new Worker();
        worker.setWname("孙发");
        worker.setAge(200);
        worker.setSex("男");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.insertWorker2(worker);
        System.out.println(row);
    }

    @Test
    public void testMapperAdd() throws Exception {
        Worker worker = new Worker();
        // 主键 整数自增, 字符串uuid , 雪花算法
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("孙照发");
        worker.setAge(20);
        worker.setSex("男");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.insertWorker (worker);
        System.out.println(row);
    }


    @Test
    public void testMapperGetbyAge() throws Exception {
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);

        List<Worker> workers = workerMapper.selectByAge(50);

        workers.forEach(System.out::println);
    }

    // 每次单词测试之前 都会执行
    //相当于getcount（）
    @Before
    public void before() throws Exception {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        // 默认不会自动提交.
        sqlSession = sqlSessionFactory.openSession();
    }

    //每次单词测试之后 都会执行
    //相当于close
    @After
    public void after() throws Exception {
        sqlSession.commit();
        sqlSession.close();
    }

//已废除
//    @Test
//    public void testUpdate() throws Exception {
//        // 演示一个 修改.
//        Worker worker = new Worker();
//        worker.setAge(500);
//        worker.setWid("5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
//        int row = sqlSession.update("worker.updateAgeByID", worker);
//        System.out.println(row);
//    }


}
